/*
 * Copyright 2007. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the GNU Lesser General Public License, Version 2. You
 * can find a copy of the license at:
 * 
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */
package com.sinai.mshab.client.ui.aggregate;

import com.sinai.mshab.client.util.Answer;
import com.sinai.mshab.client.ui.FormComponent;

/**
 * This is a conditional widget. It contains two widgets: a condition widget,
 * and a show widget.
 * 
 * The show widget is displayed if the value of the condition widget satisfies
 * some condition.
 * 
 * This widget can only have 1 condition which can be satisfied.
 * 
 * @author serge
 * 
 */
public class SingleConditionComponent extends ConditionComponent {

	/**
	 * Creates a conditional component.
	 * 
	 * @param condition_widget
	 *            the first widget based on which widget2 is showed or not
	 * @param show_widget
	 *            the second widget, is not visible until the right condition is
	 *            met
	 * @param con
	 *            the value widget1 has to return for widget2 to appear.
	 * @param vertical
	 *            specifies whether widget2 should go below (vertical) or to the
	 *            right (not vertical) of widget1.
	 */
	public SingleConditionComponent(FormComponent condition_widget,
			FormComponent show_widget, String con, boolean vertical) {
		String conditions[] = { con };
		initializeConditionComponent(condition_widget, show_widget, conditions,
				vertical);
	}

	/**
	 * Creates a vertical condition component.
	 * 
	 * @param condition_widget
	 *            the first widget based on which widget2 is showed or not
	 * @param show_widget
	 *            the second widget, is not visible until the right condition is
	 *            met
	 * @param con
	 *            the value widget1 has to return for widget2 to appear.
	 */
	public SingleConditionComponent(FormComponent condition_widget,
			FormComponent show_widget, String con) {
		String conditions[] = { con };
		initializeConditionComponent(condition_widget, show_widget, conditions,
				true);
	}

	/**
	 * Populate this widget with FormData using an Answer object
	 * 
	 * @param answer
	 *            the answer object containing the data.
	 */
	public boolean populate(Answer answer) {
		boolean success = false;

		FormComponent showWidget = getShowWidget();
		if (answer != null) {
			success = showWidget.populate(answer);
		}
		// if the sub widget is aware of it's table name, let it populate itself
		else {
			success = showWidget.populate();
		}

		if (success) {
			getConditionWidget().setAnswer(getCondition()[0]);
			receiveNotification(getConditionWidget(), this);
		}

		return success;
	}

	/**
	 * Populate this widget with FormData using a Table name
	 */
	public boolean populate() {
		return populate(null);
	}

	/**
	 * @see FormComponent#clone()
	 */
	public Object clone() {
		FormComponent w1new, w2new;
		w1new = (FormComponent) getConditionWidget().clone();
		w2new = (FormComponent) getShowWidget().clone();

		// clone the SingleConditionComponent but use
		return new SingleConditionComponent(w1new, w2new, getCondition()[0],
				isVertical());
	}
}
